package com.example.yichuangyun.ui.activity.index;

import android.text.TextUtils;
import android.widget.TextView;

import com.example.yichuangyun.R;
import com.example.yichuangyun.application.MyApplication;
import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.databinding.ActivityListBinding;
import com.example.yichuangyun.dialog.MyDialog;
import com.example.yichuangyun.http.HttpRespond;
import com.example.yichuangyun.http.MyObserver;
import com.example.yichuangyun.http.RetrofitHelper;
import com.example.yichuangyun.model.index.ClueContendModel;
import com.example.yichuangyun.mvvm.base.BaseActivity;
import com.example.yichuangyun.mvvm.base.BaseViewModel;
import com.example.yichuangyun.ui.adapter.EmptyRecyclerCommonAdapter;
import com.example.yichuangyun.ui.adapter.RecyclerViewHolder;
import com.example.yichuangyun.utils.CommonUtil;
import com.example.yichuangyun.utils.ToastHelper;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 商机竞选
 */
public class BusinessCampaignActivity extends BaseActivity<ActivityListBinding, BaseViewModel> {

    private MyDialog myDialog;
    private List<ClueContendModel> data = new ArrayList<>();
    private EmptyRecyclerCommonAdapter<ClueContendModel> adapter;

    @Override
    protected BaseViewModel bindModel() {
        return getViewModel(this, BaseViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void initData() {
        initTitleBar(getString(R.string.business_campaign));
        myDialog = new MyDialog(this);
        binding.smartRefreshLayout.setEnableLoadMore(false);
        binding.smartRefreshLayout.setEnableOverScrollDrag(true);
        getData();
    }

    private void getData() {
        JSONObject requestDate = new JSONObject();
        try {
            requestDate.put("zuoxi", MyApplication.manager.getJobNumber());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), requestDate.toString());
        RetrofitHelper.getInstance().execute(RetrofitHelper.getService().getOtherClue(requestBody), new MyObserver<HttpRespond<List<ClueContendModel>>>() {
            @Override
            protected void onSuccess(HttpRespond<List<ClueContendModel>> listHttpRespond) {
                if (Constant.SUCCESS == listHttpRespond.code) {
                    data = listHttpRespond.data;
                    adapter.onRefresh(data);
                }
                binding.smartRefreshLayout.finishRefresh();
            }
        });
    }

    @Override
    public void initCtrl() {
        adapter = new EmptyRecyclerCommonAdapter<ClueContendModel>(context, R.layout.item_campaign, data, getString(R.string.no_business_campaign)) {
            @Override
            public void convert(RecyclerViewHolder holder, ClueContendModel item, int position) {
                holder.setText(R.id.tv_source, !TextUtils.isEmpty(item.getCustomerSource()) ? item.getCustomerSource() : getString(R.string.no_from));
                holder.setText(R.id.tv_create_time, CommonUtil.checkText(item.getCreateTime()));
                holder.setText(R.id.tv_bns_id, CommonUtil.checkText(item.getId()));
                holder.setText(R.id.tv_phoneNumber, CommonUtil.checkText(item.getCustomerTel()));
                holder.setText(R.id.tv_follow_detail, !TextUtils.isEmpty(item.getSourceMemo()) ? item.getSourceMemo() : getString(R.string.no_des));
                TextView tvJoin = holder.getView(R.id.tv_join);
                if (item.getMyGrabState() == 1) {
                    tvJoin.setText(getString(R.string.join_campaign_wait_result));
                    tvJoin.setBackgroundResource(R.drawable.bg_light_426fff_2dp);
                    tvJoin.setTextColor(0xFF828282);
                    tvJoin.setEnabled(false);
                } else if (item.getMyGrabState() == 2) {
                    tvJoin.setText(getString(R.string.campaign_success));
                    tvJoin.setBackgroundResource(R.drawable.bg_light_426fff_2dp);
                    tvJoin.setTextColor(0xFF426FFF);
                    tvJoin.setEnabled(false);
                } else {
                    tvJoin.setText(getString(R.string.join_campaign));
                    tvJoin.setBackgroundResource(R.drawable.shape_button_bg_sel);
                    tvJoin.setTextColor(0xFFFFFFFF);
                    tvJoin.setEnabled(true);
                }
                holder.setOnClickListener(R.id.tv_join, v -> {
                    if (MyApplication.manager.getYcyCampaign() == 1) {
                        campaign(item.getCustomerSource(), item.getId(), item.getCustomerSourceNum());
                    } else {
                        ToastHelper.show(getString(R.string.no_campaign_permission));
                    }
                });
            }
        };

        binding.recyclerView.setAdapter(adapter);
    }

    /**
     * 竞选商机
     */
    private void campaign(String source, String id, int customerSourceNum) {
        RetrofitHelper.getInstance().execute(source.equals(getString(R.string.mdd))
                        ? RetrofitHelper.getService().campaignMdd(id)
                        : RetrofitHelper.getService().campaign(id, customerSourceNum),
                new MyObserver<HttpRespond<String>>() {
                    @Override
                    protected void onSuccess(HttpRespond<String> stringHttpRespond) {
                        if (Constant.SUCCESS == stringHttpRespond.code) {
                            getData();
                        } else {
                            myDialog.setContent(stringHttpRespond.msg + "");
                            myDialog.setCancelHide();
                            myDialog.showDialog();
                        }
                    }
                });
    }

    @Override
    protected void initClick() {
        binding.smartRefreshLayout.setOnRefreshListener(refreshLayout -> getData());
    }
}